package com.orchard.xpz.utils;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

/**
 * @TaskCode:
 * @description: 返回信息封装类
 * @author: minshuai.chen
 * @create: 2022/4/13 19:50
 **/
@Data
@NoArgsConstructor
@ToString
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;


    private int code;

//    private boolean success;

    private T data;

    private String msg;

    public R(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;

    }

    /**
     *  成功返回数据
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> success(T data){
        return new R<>(200,data,"success");
    }

    /**
     * 成功返回数据， 并定义信息
     * @param data
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> R<T> success(T data, String msg){
        return new R<>(200,data,msg);
    }

    /**
     * 成功，返回定义信息
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> R<T> success(String msg){
        return new R<>(200,null,msg);
    }

    /**
     * 定义失败返回信息
     * @param code
     * @param data
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> R<T> fail(int code, T data, String msg){
        return new R<>(code,data,msg);
    }

    /**
     * 定义失败 返回状态码，返回信息
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> R<T> fail(int code, String msg){
        return fail(code,null,msg);
    }

    /**
     * 失败返回信息
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> R<T> fail(String msg){
        return fail(500,null,msg);
    }

    /**
     * 定义失败返回数据
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> fail(T data){
        return fail(500,data,null);
    }

    /**
     * 定义失败返回数据和信息
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> fail(T data, String msg){
        return fail(500,data,msg);
    }
}
